package tech.runchen.gz5055.oms.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import tech.runchen.gz5055.oms.common.utils.OrderUtils;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Query;
import tech.runchen.gz5055.oms.common.utils.StringUtils;
import tech.runchen.gz5055.oms.modules.system.entity.OrderEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.OrderMapper;
import tech.runchen.gz5055.oms.modules.system.service.IOrderDetailService;
import tech.runchen.gz5055.oms.modules.system.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 订单信息表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-10-10
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {

    @Autowired
    private IOrderDetailService orderDetailService;

    @Override
    public PageInfo queryPage(Map<String, Object> params) {

        String orderNumber = (String) params.get("orderNumber");
        String deliveryAddress = (String) params.get("deliveryAddress");
        Object status = params.get("status");
        Object orderType = params.get("orderType");

        Object userType = params.get("userType");
        Object userId = params.get("userId");

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
                        .in(Objects.nonNull(status), "status", status)
                        .and(Integer.valueOf(userType.toString()) == 0, wrapper ->
                                wrapper.eq("order_user_id", userId).or().eq("order_type", 1)
                        )
                        .like(StringUtils.isNotEmpty(orderNumber), "order_number", orderNumber)
                        .like(StringUtils.isNotEmpty(deliveryAddress), "delivery_address", deliveryAddress)
                        .eq(Objects.nonNull(orderType), "order_type", orderType)
                        .orderByAsc("status")
                        .orderByDesc("create_time")
        );

        return new PageInfo(page);
    }

    @Override
    public List<OrderEntity> querySelect(Map<String, Object> params) {

        String orderNumber = (String) params.get("orderNumber");
        String deliveryAddress = (String) params.get("deliveryAddress");
        Object status = params.get("status");
        Object orderType = params.get("orderType");

        Object userType = params.get("userType");
        Object userId = params.get("userId");

        List<OrderEntity> list = this.list(new QueryWrapper<OrderEntity>()
                .eq(Objects.nonNull(status), "status", status)
                .and(Integer.valueOf(userType.toString()) == 0, wrapper ->
                        wrapper.eq("order_user_id", userId).or().eq("order_type", 1)
                )
                .like(StringUtils.isNotEmpty(orderNumber), "order_number", orderNumber)
                .like(StringUtils.isNotEmpty(deliveryAddress), "delivery_address", deliveryAddress)
                .eq(Objects.nonNull(orderType), "order_type", orderType)
                .orderByAsc("status")
                .orderByDesc("create_time")
        );

        return list;
    }

    @Override
    public PageInfo queryUnshippedOrder(Map<String, Object> params) {

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
                        .in("status", 0, 1)
                        .orderByAsc("status")
                        .orderByAsc("create_time")
        );
        return new PageInfo(page);
    }

    @Override
    public PageInfo queryUnreceivedOrder(Map<String, Object> params) {

        Object userId = params.get("userId");

        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
                        .in("status", 2)
                        .and(wrapper ->
                                wrapper.eq("order_user_id", userId).or().eq("order_type", 1)
                        )
                        .orderByAsc("status")
                        .orderByAsc("create_time")
        );
        return new PageInfo(page);
    }

    @Override
    public boolean saveOrder(OrderEntity order) {
        order.setOrderNumber(OrderUtils.generateOrderNumber(order.getOrderUserId()));
        this.save(order);
        if (StringUtils.isNotNull(order.getOrderDetailList())) {
            orderDetailService.saveOrderDetailBatch(order.getId(), order.getOrderDetailList());
        }
        return true;
    }

    @Override
    public boolean updateOrder(OrderEntity order) {
        return this.updateById(order);
    }

    @Override
    public void deleteOrder(Long orderId) {
        this.removeById(orderId);
        orderDetailService.deleteByOrderIdBatch(orderId);
    }
}
